package com.leetcode.partition7;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/24 9:15
 */
public class LC661图片平滑器 {

    private static final int[][] dirs = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
    private static int[][] img;
    private static int n, m;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        int n = Integer.parseInt(ss[0]), m = Integer.parseInt(ss[1]);
        int[][] img = new int[n][m];
        for (int i = 0; i < n; i++) {
            ss = reader.readLine().split(" ");
            for (int j = 0; j < m; j++) {
                img[i][j] = Integer.parseInt(ss[j]);
            }
        }
        writer.write(Arrays.deepToString(imageSmoother(img)));
        writer.flush();
    }

    public static int[][] imageSmoother(int[][] _img) {
        n = _img.length;
        m = _img[0].length;
        img = _img;
        int[][] ans = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i][j] = get(i, j);
            }
        }
        return ans;
    }

    private static int get(int x, int y) {
        int s = 0, cnt = 0;
        for (int[] dir : dirs) {
            int nx = x + dir[0], ny = y + dir[1];
            if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
            s += img[nx][ny];
            cnt++;
        }
        return s / cnt;
    }
}
